const app = getApp()

Page({
  data: {
    banner_list: [],
    categoryList:[],
    nowCategory:0,
    itemLists: [],
    flags: {},
    zhufuFlag:false
  },

  onLoad: function() {
    const updateManager = wx.getUpdateManager()
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    
    this.getConfig()
    this.getBanner() 
    
    this.getCategory()
  },

  // 获取banner
  getBanner: function () {
    var that = this
    wx.request({
      url: app.globalData.url + 'banner?version='+ app.globalData.version,
      success: function (res) {
        that.setData({
          banner_list: res.data.data
        })
      }
    })
  },

  // 获取config
  getConfig: function() {
    var that = this
    wx.request({
      url: app.globalData.url + 'config?version=' + app.globalData.version,
      success: function(res) {
        if (res.data.data) {
          that.setData({
            flags: res.data.data,
          })
        }
      }
    })
  },

  // 获取分类
  getCategory: function(){
    wx.request({
      url: app.globalData.url + 'category?version=' + app.globalData.version,
      success: (res)=>{
        this.setData({
          categoryList: res.data.data
        })
        this.getListByType(res.data.data[0].id , app.globalData.version);        
      },
    });
  },

  // 更改分类
  changeCategory: function(e){
    var that = this
    var type = e.currentTarget.dataset.type
    that.setData({
      nowCategory: e.currentTarget.dataset.category
    })
    that.getListByType(type , app.globalData.version);
  },

  onShareAppMessage: function () { },  

  // 根据分类type获取对应数据list
  getListByType: function(type , version){
    var that = this
    wx.request({
      url: app.globalData.url + 'get_list',
      data: {
        type: type,
        version: version
      },
      method: 'GET',
      success: (res)=>{
        if(res.data.data.zhufu){
          that.setData({
            zhufuFlag: res.data.data.zhufu
          })
        }else{
          that.setData({
            zhufuFlag: false
          })
        }
        that.setData({
          itemLists: res.data.data.list
        })
      }
    });
  },

})